﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using Microsoft.Xna.Framework.Graphics;

namespace sand_game
{
    static class bitmapWriter
    {
            static byte[] textureData;
            static Bitmap bmp;
            static BitmapData bitmapData;
            static IntPtr safePtr;
            static Rectangle rect;
            public static ImageFormat imageFormat;
            public static void init(int width, int height)
            {
                textureData = new byte[4 * width * height];

                bmp = new System.Drawing.Bitmap(
                               width, height,
                               System.Drawing.Imaging.PixelFormat.Format32bppArgb
                             );

                rect = new System.Drawing.Rectangle(0, 0, width, height);
                imageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
            }
            public static void TextureToBmp(Texture2D texture, String filename)
            {
                texture.GetData<byte>(textureData);
                byte blue;
                for (int i = 0; i < textureData.Length; i += 4)
                {
                    blue = textureData[i];
                    textureData[i] = textureData[i + 2];
                    textureData[i + 2] = blue;
                }

                bitmapData = bmp.LockBits(
                               rect,
                               System.Drawing.Imaging.ImageLockMode.WriteOnly,
                               System.Drawing.Imaging.PixelFormat.Format32bppArgb
                             );

                safePtr = bitmapData.Scan0;
                System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
                bmp.UnlockBits(bitmapData);
                bmp.Save(filename, imageFormat);

            }

    }
}
